<template>
  <div class="print">
    <el-button v-if="detail.status < 5" type="danger" class="success btn" @click="handleClick">打印</el-button>
    <el-button type="success" class="btn" @click="hanleBack">返回上一页</el-button>
    <el-dialog
      title="套餐小票打印信息"
      :visible.sync="packagePrint"
      :append-to-body="true"
      style="padding: 0"
      class="print-dialog"
      width="300px"
    >
      <section id="print-html">
        <div class="print-img"><img src="../../assets/avatar.png"></div>
        <div class="title">欢迎使用驴优客</div>
        <div class="star"><span v-for="(item,index) of 26" :key="index">*</span></div>
        <div v-if="detail.shop_name" class="desc">门店： {{ detail.shop_name.toString().substr(0, 14) }}</div>
        <div class="desc">用户： {{ detail.username.toString().substr(0,3) }}****{{ detail.username.toString().substr(-4) }}</div>
        <div v-if="detail.order_no" class="desc">订单号：{{ detail.order_no.substr(0, 14) }}</div>
        <div class="desc">交易时间：{{ parseTimeFuc(detail.create_at) }}</div>
        <div class="star"><span v-for="(item,index) of 26" :key="index">*</span></div>
        <table width="100%" class="table">
          <tr class="print-tr">
            <th class="name">名称</th>
            <th v-show="type !== 7">价格</th>
            <th>数量</th>
            <th v-if="type !== 7">金额</th>
            <th v-else>房时</th>
          </tr>
          <tr class="print-tr">
            <td>{{ detail.order_name ? detail.order_name : detail.package_name }}</td>
            <td v-show="type !== 7">{{ detail.amount }}</td>
            <td>{{ detail.count ? detail.count : detail.exchange_day }}</td>
            <td>{{ detail.amount? detail.amount : detail.used_quantity }}<span v-show="type === 7">小时</span></td>
          </tr>
        </table>
        <div class="prop">
          <p v-if="type !==7">合计金额：</p>
          <p v-else>使用房时：</p>
          <p>{{ detail.amount ? detail.amount : detail.used_quantity }}<span v-show="type === 7">小时</span></p>
        </div>
        <div class="prop">
          <p>支付状态</p>
          <p v-show="type !== 7">已支付</p>
          <p v-show="type === 7">已兑换</p>
        </div>
        <div class="star"><span v-for="(item,index) of 26" :key="index">*</span></div>
        <!-- <p class="desc1">补打印</p> -->
        <p class="desc1">此小票仅供对账存留</p>
        <p class="desc1">感谢您的使用，谢谢！</p>
      </section>
      <span slot="footer" class="dialog-footer">
        <el-button @click="packagePrint = false">取 消</el-button>
        <el-button type="primary" @click="handlePrint">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog
      v-if="type===6"
      title="套餐小票打印信息"
      :visible.sync="singleOrder"
      :append-to-body="true"
      style="padding: 0"
      class="print-dialog"
      width="300px"
    >
      <section id="print-html1">
        <div class="print-img"><img src="../../assets/avatar.png"></div>
        <div class="title">欢迎使用驴优客</div>
        <div class="star"><span v-for="(item,index) of 26" :key="index">*</span></div>
        <div class="desc">门店： {{ detail.shop_name.toString().substr(0, 14) }}</div>
        <div class="desc">用户： {{ detail.username.toString().substr(0,3) }}****{{ detail.username.toString().substr(-4) }}</div>
        <div class="desc">订单号：{{ detail.order_no.toString().substr(0, 14) }}</div>
        <div class="desc">交易时间：{{ parseTimeFuc(detail.create_at) }}</div>
        <div class="star"><span v-for="(item,index) of 26" :key="index">*</span></div>
        <table v-if="detail.store.length" width="100%" class="table">
          <tr class="print-tr">
            <th class="name">名称</th>
            <th class="name1">规格</th>
            <th>单价</th>
            <th>数量</th>
          </tr>
          <tr v-for="item of detail.store" :key="item.goods_id" class="print-tr">
            <td>{{ item.goods_name }}</td>
            <td class="name1">{{ item.spec }}</td>
            <td>{{ item.price }}</td>
            <td>{{ item.count }}</td>
          </tr>
        </table>
        <div class="prop">
          <p>合计金额：</p>
          <p>{{ detail.amount }}</p>
        </div>
        <div class="prop">
          <p>支付状态</p>
          <p>已支付</p>
        </div>
        <div class="star"><span v-for="(item,index) of 26" :key="index">*</span></div>
        <!-- <p class="desc1">补打印</p> -->
        <p class="desc1">此小票仅供对账存留</p>
        <p class="desc1">感谢您的使用，谢谢！</p>
      </section>
      <span slot="footer" class="dialog-footer">
        <el-button @click="singleOrder = false">取 消</el-button>
        <el-button type="primary" @click="handlePrint1">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import printJS from 'print-js'
import { parseTime } from '@/utils'
export default {
  name: '',
  props: {
    type: {
      type: Number,
      default: 1
    },
    info: {
      type: Object,
      default() {
        return {}
      }
    }
  },
  data() {
    return {
      packagePrint: false,
      parseTimeFuc: parseTime,
      singleOrder: false,
      detail: {}
    }
  },
  computed: {
    tableData() {
      const { info } = this
      return [{ order_name: info.order_name, count: info.count, amount: info.amount }]
    }
  },
  watch: {
    info(info) {
      console.log(info)
      this.detail = info
    }
  },
  created() {
    this.detail = this.info
  },
  methods: {
    hanleBack() {
      console.log(window.history)
      if (window.history.length < 1) {
        this.$router.push('/')
        return
      }
      this.$router.back()
    },
    handleClick() {
      if (Number(this.type) === 6) {
        console.log(111)
        this.singleOrder = true
      } else {
        this.packagePrint = true
      }
    },
    handlePrint() {
      printJS({
        printable: 'print-html',
        type: 'html',
        style: '#print-html{width: 100%}.name{width: 25%}.name1{width: 24%}.title{ font-size: 14px; text-align: center; }td{font-size: 12px; text-align: center;}.table th{font-size: 12px; font-weight: normal}.print-img{width: 100%;text-align: center; } img{;width: 100px; height: 100px}.desc{font-size: 12px; width:100%;transform：scale(0.8)}.prop{font-size: 12px;display:flex;justify-content: space-between;padding:0 4px;box-sizing:border-box; line-height: 16px}.desc1{text-align: center; font-size: 10px}',
        scanStyles: false
      })
    },
    handlePrint1() {
      printJS({
        printable: 'print-html1',
        type: 'html',
        style: '#print-html1{width: 100%}.name{width: 25%}.name1{width: 24%}.title{ font-size: 14px; text-align: center; }td{font-size: 12px;text-align: center;}.table th{font-size: 12px; font-weight: normal}.print-img{width: 100%;text-align: center; } img{;width: 100px; height: 100px}.desc{font-size: 12px; width:100%;transform：scale(0.8)}.prop{font-size: 12px;display:flex;justify-content: space-between;padding:0 4px;box-sizing:border-box; line-height: 16px}.desc1{text-align: center; font-size: 10px}',
        scanStyles: false
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.print {
  display: flex;
  padding-top: 20px;
  box-sizing: border-box;
  width: 100%;
  justify-content: center;
  .success{
    margin-right: 100px;
  }
  .btn{
    width: 100px;
    height: 50px;
  }
}
#print-html, #print-html1 {
  padding: 20px;
  box-sizing: border-box;
  width: 300px;
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
  img{
    width: 100px;
    height: 100px;
  }
  .desc{
    width: 100%;
    line-height: 20px;
  }
  .prop{
    display: flex;
    width: 100%;
    justify-content: space-between;
  }
  .print-tr{
    padding:4px;
  }
  table{
    position: relative;
    margin-bottom: 10px;
  }
  table::before {
    width: 100%;
    height: 1px;
    content: "";
    position: absolute;
    background-color: #ebeef5;
    z-index: 1;
    bottom: -4px;
  }
  td{
    text-align: center;
    max-width: 100px;
    width: 100px;
  }
  .star{
    margin: 10px 0;
  }
}
</style>
